package com.xblog.chatbox.vlmp.core.dify;


import com.xblog.chatbox.vlmp.conversation.Conversation;
import com.xblog.chatbox.vlmp.conversation.ConversationPayloadFeedbacks;
import com.xblog.chatbox.vlmp.conversation.ConversationPayloadRating;
import com.xblog.chatbox.vlmp.core.VlmpAgentClient;
import com.xblog.chatbox.vlmp.core.VlmpAgentFeedbacks;
import com.xblog.chatbox.vlmp.core.VlmpAgentRequest;
import com.xblog.chatbox.vlmp.core.spec.CallResponseSpec;
import com.xblog.chatbox.vlmp.core.spec.StreamResponseSpec;
import com.xblog.chatbox.vlmp.core.spec.StringCallResponseSpec;
import com.xblog.chatbox.vlmp.core.spec.StringStreamResponseSpec;
import com.xiaowu.xblog.constant.UserContext;
import okhttp3.OkHttpClient;
import org.apache.tomcat.util.http.parser.Authorization;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

/**
 * @description:
 * @author: xiaowu
 * @time: 2025/8/6 22:44
 */
public class DifyAgentClient implements VlmpAgentClient {

    private static final Logger logger = LoggerFactory.getLogger(DifyAgentClient.class);

    private final DifyAgentApi api;

    public DifyAgentClient(OkHttpClient client,
                           DifyProperties properties,
                           DifyAgent agent) {
        this.api = DifyAgentApi.builder()
                .client(client)
                .properties(properties)
                .agent(agent)
                .build();
    }

    @Override
    public StreamResponseSpec stream(UserContext authorization, VlmpAgentRequest request) {
        try {
            return StringStreamResponseSpec.of(api.stream(request));
        } catch (Exception e){
            logger.error(e.getMessage(), e);
        }
        return StringStreamResponseSpec.EMPTY_SPEC;
    }

    @Override
    public CallResponseSpec call(UserContext userContext, VlmpAgentRequest request) {
        try {
            return StringCallResponseSpec.of(api.call(request));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return StringCallResponseSpec.EMPTY_SPEC;
    }

    @Override
    public void delete(UserContext context, Conversation conversation) {
        try {
            if (!StringUtils.hasText(conversation.getConversationId())) {
                return;
            }
            VlmpAgentRequest message = VlmpAgentRequest.builder()
                    .user(context.getUsername())
                    .conversationId(conversation.getConversationId())
                    .build();
            api.delete(message);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }

    @Override
    public void rename(UserContext context, Conversation conversation) {
        try {
            if (!StringUtils.hasText(conversation.getConversationId()) ) {
                return;
            }
            VlmpAgentRequest message = VlmpAgentRequest.builder()
                    .user(context.getUsername())
                    .conversationId(conversation.getConversationId())
                    .conversationName(conversation.getName())
                    .build();
            api.rename(message);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }

    @Override
    public void feedbacks(UserContext authorization, ConversationPayloadFeedbacks feedbacks) {
        try {
            VlmpAgentFeedbacks request = VlmpAgentFeedbacks.builder()
                    .messageId(feedbacks.getMessageId())
                    //点赞 like, 点踩 dislike, 撤销点赞 null
                    .rating(ConversationPayloadRating.rating.equals(feedbacks.getRating()) ? null : feedbacks.getRating().name())
                    .user(authorization.getUsername())
                    .description(feedbacks.getDescription())
                    .build();
            api.feedbacks(request);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }
}
